package com.ece.ecofitness.controller;

import java.util.Date;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.ece.ecofitness.model.Commande;
import com.ece.ecofitness.service.ArticleService;
import com.ece.ecofitness.service.CommandeService;
import com.ece.ecofitness.service.UserService;

@Controller
public class CommandController {

	@Autowired
	private CommandeService commandeService;

	@Autowired
	private UserService userService;
	
	@Autowired
	private ArticleService articleService;
	

	@RequestMapping(value = "/command/get.view", method = RequestMethod.GET)
	public String getCommand(Model model,HttpServletRequest request, HttpServletResponse response) {
		String login=null;
		for(Cookie c: request.getCookies()) {
	        if(c.getName().equals("login"))
	        {
	        	login=c.getValue();
	        	break;
	        }
	    }
		if(login!=null)
		{
			Commande command= commandeService.getCommande(Integer.parseInt(request.getParameter("id")));
			if(command.getDateCommande()!=null)
				model.addAttribute("commandCheck",true );
			if(command.getDateLivraison()!=null)
				model.addAttribute("livraisonCheck",true );
			if(command.getDateReception()!=null)
				model.addAttribute("receptionCheck",true );
			model.addAttribute("command",command );
			return "command";
		}
		else
			return "home";
	}
	
	@RequestMapping(value = "/command/update.view", method = RequestMethod.POST)
	public String updateCommand(Model model,HttpServletRequest request, HttpServletResponse response) {
		String login=null;
		for(Cookie c: request.getCookies()) {
	        if(c.getName().equals("login"))
	        {
	        	login=c.getValue();
	        	break;
	        }
	    }
		if(login!=null)
		{
			int idCommand=Integer.parseInt(request.getParameter("idCommand"));
			Commande command= commandeService.getCommande(idCommand);
			Date dateLivraison=null;Date dateReception=null;
			if(request.getParameter("livraison")!=null &&request.getParameter("livraison").equals("livre") && (command.getDateLivraison()==null))
				dateLivraison=new Date();
			if(request.getParameter("reception")!=null && request.getParameter("reception").equals("receptionne") && ( command.getDateReception()==null))
				dateReception=new Date();
			
			Commande cmd=commandeService.updateCommande(idCommand, dateLivraison, dateReception);
			if(cmd==null){
				model.addAttribute("error","Updating not success!");
				if(command.getDateCommande()!=null )
					model.addAttribute("commandCheck",true );
				if(command.getDateLivraison()!=null )
					model.addAttribute("livraisonCheck",true );
				if(command.getDateReception()!=null )
					model.addAttribute("receptionCheck",true );
				model.addAttribute("command",command );
				
			}
			else{
				model.addAttribute("error","Updating success!");
				if(cmd.getDateCommande()!=null )
					model.addAttribute("commandCheck",true );
				if(cmd.getDateLivraison()!=null )
					model.addAttribute("livraisonCheck",true );
				if(cmd.getDateReception()!=null )
					model.addAttribute("receptionCheck",true );
				model.addAttribute("command",cmd );
			}
			return "command";
		}
		else
			return "home";
	}
}
